﻿using IService.Admin;
using Model.Parameter.Admin;

namespace Service.Admin
{
    public class RecycleReqHandleService : BaseService<TRecycleReqHandle>, IRecycleReqHandleService
    {
        public RecycleReqHandleService(IUnitOfWorkService unitOfWorkService) : base(unitOfWorkService)
        {
        }

        /// <summary>
        /// 添加或修改
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        public async Task<RM_ApiResult> AddOrUpdate(TRecycleReqHandle par)
        {
            if (par.Id == 0)
            {
                par.HandleTime = DateTime.Now;
                par.AdminId = _CurrentAdmin.Id;
            }

            var result = await AddOrUpdate(par, true);

            if (result > 0)
            {
                await Status<TRecycleReq>(par.ReqId, par.Status);

                return _ResultHelper.BuildActionSuccess();
            }
            else
            {
                return _ResultHelper.BuildActionError();
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        public async Task<RM_ApiResult> GetPage(Par_AdminPageBase par)
        {
            var exp = Expressionable.Create<TRecycleReq>().And(o => o.IsDeleted == false)
                .AndIF(!string.IsNullOrWhiteSpace(par.KeyWord), o => o.ProductName.Contains(par.KeyWord));

            var where = exp.ToExpression();
            var datas = new RM_PageReturn<TRecycleReq>
            {
                TotalCount = await _DbContext.Queryable<TRecycleReq>().Where(exp.ToExpression()).CountAsync(),
                Datas = await _DbContext.Queryable<TRecycleReq>().Includes(u => u.Customer).Where(where).OrderBy(o => o.Status).OrderByDescending(o => o.Id)
                .ToPageListAsync(par.PageNo, par.PageSize),
                PageSize = par.PageSize,
                PageNo = par.PageNo
            };

            datas.Datas.ForEach(item =>
            {
                if (item.Customer != null)
                {
                    item.MemberName = item.Customer.NickName;
                }

                item.StatusDesc = _EnumHelper.GetDescription((Enum_RecycleStatus)item.Status);
            });

            return _ResultHelper.BuildSuccess(datas);
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public new async Task<RM_ApiResult> GetItem(int key)
        {
            if (await Any<TRecycleReq>(key))
            {
                var o = await _DbContext.Queryable<TRecycleReq>().Includes(u => u.Customer).FirstAsync(o => o.Id == key);
                if (o != null)
                {
                    if (o.Customer != null)
                    {
                        o.MemberName = o.Customer.NickName;
                    }

                    o.StatusDesc = _EnumHelper.GetDescription((Enum_RecycleStatus)o.Status);
                    o.RecycleReqHandle = await _DbContext.Queryable<TRecycleReqHandle>().Includes(u => u.AdminInfo).Where(o => o.ReqId == key).OrderByDescending(o => o.HandleTime).FirstAsync();

                    if (o.RecycleReqHandle != null)
                    {
                        o.HandleBy = o.RecycleReqHandle.AdminInfo.AdminName;
                        o.HandleTime = o.RecycleReqHandle.HandleTime;
                        o.HandleDesc = o.RecycleReqHandle.HandleDesc;
                        o.HandlePicture = o.RecycleReqHandle.HandlePicture;
                        o.HandleAttachment = o.RecycleReqHandle.HandleAttachment;
                        o.RecycleReqHandle = null;
                    }

                    if (o.Status > (int)Enum_RecycleStatus.PendingReview)
                    {
                        o.RecycleSend = await _DbContext.Queryable<TRecycleSend>().Where(o => o.ReqId == key).FirstAsync();
                        if (o.RecycleSend != null)
                        {
                            o.RecycleSendHandle = await _DbContext.Queryable<TRecycleSendHandle>().Includes(u => u.AdminInfo).Where(x => x.ReqId == o.Id).OrderByDescending(x => x.HandleTime).FirstAsync();
                            if (o.RecycleSendHandle != null)
                            {
                                o.SendHandleBy = o.RecycleSendHandle.AdminInfo.AdminName;
                                o.SendHandleTime = o.RecycleSendHandle.HandleTime;
                                o.SendHandleDesc = o.RecycleSendHandle.HandleDesc;
                                o.SendHandlePicture = o.RecycleSendHandle.HandlePicture;
                                o.SendHandleAttachment = o.RecycleSendHandle.HandleAttachment;
                                o.SendHandleCoupon = o.RecycleSendHandle.Coupon;
                                o.RecycleSendHandle = null;
                            }
                        }
                    }
                }

                return _ResultHelper.BuildSuccess(o);
            }
            else
            {
                return _ResultHelper.BuildError(Enum_SysAttributeType.Misc.NoData);
            }
        }

        public new async Task<RM_ApiResult> DeleteItem(int key)
        {
            await SoftDelete<TRecycleReq>(key);
            return _ResultHelper.BuildActionSuccess();
        }
    }
}